package dd.prog2.exercises.set10;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;

public class DuplicateFileFinder {
	/*
	 * Aufgabe 5 funktioniert nicht richtig. Nach der Suche wird nichts zurueckgegeben
	 */
	public static void main(String[] args) {
		FileProcessor fp = new FileProcessor();
		FlexibleFileTreeWalker mytw = new FlexibleFileTreeWalker(fp);

		try {
			mytw.walk(".");
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("smth wrong");
		}
//		fp.sort();
		fp.compare();
		fp.printMap();
	}

	static class FileProcessor implements FlexibleFileTreeWalker.FileProcessor {
		private Map <String, Long> file_list = new HashMap<String, Long>();

		public FileProcessor() {}
		
		@Override
		public void process(File file) throws IOException {
//			System.out.printf("%6d %-100s%n", file.length(), file.getCanonicalPath());
			file_list.put(file.getCanonicalPath(),file.length());
		}
		

		@SuppressWarnings({ "all" })
		public void sort() {
			List sorted_list = new LinkedList(file_list.entrySet());
			// sort with Comparator
			Collections.sort(sorted_list, 
				new Comparator() {
				public int compare(Object o1, Object o2) {
					return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
				}
			});

			for (Iterator it = sorted_list.iterator(); it.hasNext();) {
				Map.Entry entry = (Map.Entry) it.next();
				System.out.printf("%6d %-100s%n",entry.getValue(), entry.getKey());
			}
		}
		
		public void printMap(){
			System.out.println("pairs only:");
			Iterator<Entry<String, Long>> it = file_list.entrySet().iterator();
			while(it.hasNext()){
				Entry<String, Long> next = it.next();
				System.out.printf("%6d %-100s%n",next.getValue(), next.getKey());
			}
		}
		
		public String cutString(String in_string){
			String res = "";
			StringTokenizer st = new StringTokenizer(in_string, "\\");
			while(st.hasMoreTokens())
				res = st.nextToken();
			return res;
		}
		
		public void compare() {
			Iterator<Entry<String, Long>> it = file_list.entrySet().iterator();
			Iterator<Entry<String, Long>> it2 = file_list.entrySet().iterator();
			int i = 0;
			while(it.hasNext()){
				boolean has_pair = false;
				Entry<String, Long> first = it.next();
				while (it2.hasNext()) {
					Entry<String, Long> next = it2.next();
					if(first != next){ // different files?
						if(first.getValue().equals(next.getValue())){// same size?
							String f_name = cutString(first.getKey());// File.getName
							String n_name = cutString(next.getKey());// File.getName
							if(f_name.equals(n_name) && !first.getKey().equals(next.getKey())){// same name & !paths
									has_pair = true; 
							}
						}
					}
				}
				if(!has_pair){ // current file has no pair
					file_list.remove(first.getKey()); // delete current
					it = file_list.entrySet().iterator(); // reset iterator
					it2 = file_list.entrySet().iterator();
				}
			}
		}
		
	}
}
